﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;

namespace GoodMan.Portal.Biz
{
    public class NoticeBiz
    {
        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="takeSize"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public static ItemListBox<Notice> GetAllNotices(int page, int takeSize, string search = "")
        {
            using (Context ctx = new Context())
            {
                var query = from c in ctx.Notice
                            select c;

                if (!string.IsNullOrEmpty(search))//楼栋名称
                {
                    query = query.Where(c => c.Title.Contains(search)|| c.SubTitle.Contains(search));
                }
                var data = query.OrderByDescending(c => c.Id).Skip(page * takeSize).Take(takeSize).ToList().Select(c => new Notice().Set(c)).ToList();
                var itemCount = query.Count();
                return new ItemListBox<Notice>(data).BuildPage(itemCount, page, takeSize, new PageParameter()
                {
                    Style = "workspace"
                });
            }
        }

        /// <summary>
        /// 验证公告名称是否相同
        /// </summary>
        /// <param name="name">公告名称</param>
        /// <returns>true:不存在 false:存在</returns>
        public static bool IsExits(string name)
        {
            if (new Notice().Any(c => c.Title.Equals(name)))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool SaveNotice(Notice info)
        {
            return info.PostAdd() != null;
        }        
    }
}
